package com.trt.eatmeat.socket;

import com.trt.eatmeat.EatmeatApplication;
import com.trt.eatmeat.util.ContantUtils;
import com.trt.eatmeat.util.JsonUtils;
import com.trt.eatmeat.util.MsgType;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class ServerHandler extends SimpleChannelInboundHandler<MsgData> {
    public static Logger log = LoggerFactory.getLogger(ServerHandler.class);


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MsgData msg) throws Exception {
       log.info(JsonUtils.objectToJson(msg));
        MsgType type = MsgType.getType(msg.get_type());
        switch (type){
            case join:
                // 进行唯一标记对应登记
                UserChannelRel.put(msg.get_sendId(),ctx.channel().id());
                MsgData msgData = new MsgData();
                msgData.set_type(msg.get_type());
                msgData.set_receiptId(msg.get_sendId());
                msgData.set_sendId(msg.get_receiptId());
                msgData.set_jsonMsg(msg.get_jsonMsg());
                ctx.channel().writeAndFlush(Unpooled.copiedBuffer(JsonUtils.objectToJson(msgData), CharsetUtil.UTF_8));
                break;
            case http:
                if(msg.get_jsonMsg().getAlias().equals(ContantUtils.login)){
                    //
                    Map<String,Object> map = msg.get_jsonMsg().getResult();
                    EatmeatApplication.resultMap.put(msg.get_reqNo(),map);
                }else if(msg.get_jsonMsg().getAlias().equals("subscript")){
                    log.info(JsonUtils.objectToJson(msg));
                    Map<String,Object> map = msg.get_jsonMsg().getResult();
                    EatmeatApplication.resultMap.put(msg.get_reqNo(),map);
                }
                break;
            case quit:
                //发送离线请求
                break;
            case no:
                break;
        }
    }


    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        NettyServer.users.add(ctx.channel());
        log.info("有用户加入:"+ NettyServer.users.size()+"人");
        UserChannelRel.put("",ctx.channel().id());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

        String channelId = ctx.channel().id().asLongText();
        log.info("客户端被移除，channelId为：" + channelId);
        // 当触发handlerRemoved，ChannelGroup会自动移除对应的客户端channel
        NettyServer.users.remove(ctx.channel());
        UserChannelRel.remove(ctx.channel().id());
        UserChannelRel.output();
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
                 //异常发生时关闭上下文对象
                NettyServer.users.remove(ctx.channel());
                UserChannelRel.remove(ctx.channel().id());
                log.info("异常被移除，channelId为：" + ctx.channel().id().asLongText());
                log.error("异常",cause);
                UserChannelRel.output();
                 ctx.close();
             }
}